package com.web.base.auth.provider;

import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.auth.cache.AppCache;
import com.web.base.auth.exception.AppException;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.message.AppRequest;
import com.web.base.auth.message.AppResponse;
import com.web.base.auth.model.App;
import com.web.base.auth.service.AppService;
import com.web.base.auth.util.AppConvertor;
import com.web.core.async.redis.CacheRefreshSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("authAppProvider")
public class AppProvider implements AppFacade
{
	@Autowired
	private AppService appService;
	@Autowired
	private AppCache appCache;
	@Resource
	private CacheRefreshSender cacheRefreshSender;
	
	public AppProvider()
	{
	}

	@Override
	public AppResponse add(AppRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		App app = AppConvertor.toApp(request);
		app = appService.save(app);
		cacheRefreshSender.send(CacheRefreshSender.APP_CACHE, app.getId());
		AppResponse response = AppConvertor.toResponse(app);
		return response;
	}

	@Override
	public AppResponse update(AppRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		App app = AppConvertor.toApp(request);
		app = appService.save(app);
		cacheRefreshSender.send(CacheRefreshSender.APP_CACHE, app.getId());
		AppResponse response = AppConvertor.toResponse(app);
		return response;
	}

	@Override
	public AppResponse get(Integer id)
	{
		if (id == null)
			throw new AppException("应用ID不能为空");
		
		App app = appCache.get(id);
		AppResponse response = AppConvertor.toResponse(app);
		return response;
	}
	
	@Override
	public AppResponse getByCode(String code)
	{
		if (code == null || code.isEmpty())
			throw new AppException("应用代码不能为空");
		
		App app = appCache.getByCode(code);
		AppResponse response = AppConvertor.toResponse(app);
		return response;
	}
	
	@Override
	public Map<Integer, AppResponse> getList(Collection<Integer> idList)
	{
		if (idList == null || idList.isEmpty())
			throw new AppException("应用ID列表不能为空");
		
		Map<Integer, App> map = idList.stream()
				.map(e -> appCache.get(e))
				.filter(e -> e != null)
				.collect(Collectors.toMap(App::getId, v -> v));
		Map<Integer, AppResponse> responseMap = AppConvertor.toResponse(map);
		return responseMap;
	}

	@Override
	public Page<AppResponse> listPage(AppRequest request, int start, int size)
	{
		App app = AppConvertor.toApp(request);
		Page<App> page = new Page<>(size);
		page.setCurPage(start);
		page = appService.list(app, page);
		Page<AppResponse> responsePage = AppConvertor.toResponse(page);
		return responsePage;
	}

	@Override
	public List<AppResponse> listAll()
	{
		List<App> list = appCache.list();
		List<AppResponse> responseList = AppConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public void refresh()
	{
		cacheRefreshSender.send(CacheRefreshSender.APP_CACHE);
	}
	
}
